<?php

namespace front\modules\app\controllers;

use Yii;
use front\models\OrderModel;
use yii\data\ActiveDataProvider;
use front\controllers\WebBaseController;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use order\services\OrderService;
use goods\services\ProductService;
use yii\helpers\ArrayHelper;

/**
 * OrderController implements the CRUD actions for OrderModel model.
 */
class OrderController extends WebBaseController
{
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * Lists all OrderModel models.
     * @return mixed
     * 订单列表
     * param用户id
     * 订单状态 0:待支付 1:待出库 2:已支付 3:待发货 4:已发货,等待收货
     * -1:供应商作废 -2:客户作废 -3:系统作废 -4:全部退货 -5:部分退货 -6:待审核 -7:退款
     */
    public function actionIndex()
    {
        $data = yii::$app->request->get();
        $data['customer_id'] = $this->user_id;
        $data['status'] = yii::$app->request->get('status');
        $data['getByWeb']=1;
        $info = OrderService::findOrder($data);
        $info['result'] = ArrayHelper::toArray($info['result']);
        $arr = array_map(function($v){
            $v['order_time'] = date('Y-m-d H:i:s',$v['order_time']);
            $v['orderItem'] = OrderService::findItems(['order_id'=>$v['order_id']]);
            $v['orderItem'] = ArrayHelper::toArray($v['orderItem']);
            return $v;
        },$info['result']);
        WebBaseController::getAjaxData(1,'获取成功',$arr);
    }

    /**
     * 插入订单
     * $data = [
    0 => [
    'seller_id' => 1000001059,
    'customer_id' => 1,
    'channel_source' => 1,
    'device_source' => 1,
    'order_users_id' => 4,
    'remark' => '备注信息000',
    'express' => [
        name => 八个字
        district => 1024
        address => 宜山路200号518室
        tel => 13112345678
        mobile => 56131214
     ]
    'item' => [
        0 => [
        'product_id' => '603614611',
        'price' => 22.33,
        'buy_num' => 2,
        'data' => [
        'color' => '红色',
        'size' => '大',
        ]
        ],
        1 => [
        'product_id' => '603614611',
        'price' => 22.33,
        'buy_num' => 10,
        'data' => [
        'color' => '红色',
        'size' => '大',
        ]
        ],
    ]
    ]
    ];
     */
    function actionCreate(){
        //订单表
        $data=yii::$app->request->post();
        $data['customer_id'] = $this->user_id;
        if($data['channel_source']==2){   //线下付款
            $data['express']['user_name'] = yii::$app->request->post('user_name');
            $data['express']['user_phone'] = isset($_POST['user_phone']) ? $_POST['user_phone'] :'';
        }
        $services = new OrderService();
        $services->placeOrder($data);
        WebBaseController::getAjaxData(1,'插入成功');
    }

    /**
     * 订单支付
     */
    function actionPay(){
        $data = Yii::$app->request->post();
        $model = new OrderService();
        $model->payOrder($data);
        WebBaseController::getAjaxData(1,'插入成功');
    }

    /**
     *
     */


    /**
     * Displays a single OrderModel model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }


    /**
     * Updates an existing OrderModel model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->order_id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing OrderModel model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the OrderModel model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return OrderModel the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = OrderModel::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
